import { IToast, IRouter } from '@/types/tools'
import { IVideo } from '../types'
import dayjs from 'dayjs'

export const Toast = ({title, icon = 'none', duration = 0}:IToast) =>{
	uni.showToast({
		title,
		icon,
		duration
	})
}

export const useRouter = ({url, data = {}, type = 'navigateTo'}:IRouter) => {
	try {
		if (Object.keys(data).length) url = `${url}?data=${encodeURIComponent(JSON.stringify(data))}`
		if (type === 'navigateBack') {
			uni[type]({ delta: url as number })
		} else {
			uni[type]({ url })
		}
	} catch (error) {
		console.error(error)
	}
}

export const PromiseAll = (promise = []) => {
  return Promise.allSettled(promise).then((values) => values)
}

/**
 * 解析useRouter参数
 */
export const parsePageParams = <T = any>(params: string): T =>{
	return JSON.parse(decodeURIComponent(params))
}

/**
 * 防抖
 */
export const debounce = (fn: () => void, wait: number) => {
  let timer = null
  return function () {
    let context = this,
      args = arguments
    if (timer) {
      clearTimeout(timer)
      timer = null
    }
    timer = setTimeout(() => {
      fn.apply(context, args)
    }, wait)
  }
}

/**
 * 节流
 */
export const throttle = (fn: () => void, delay: number) => {
  let curTime = Date.now()
  return function () {
    let context = this,
      args = arguments,
      nowTime = Date.now()

    if (nowTime - curTime >= delay) {
      curTime = Date.now()
      return fn.apply(context, args)
    }
  }
}

export const getFormatMothTime = () => {
  let date = new Date()
  let today = date.getDate()
  let month = date.getMonth() + 1
  let year = date.getFullYear()
  let dayOfWeeks = new Date(year, month, 0)
  let week = new Date(year, date.getMonth(), 1).getDay()
  return {
    today,
    month,
    monthDays: dayOfWeeks.getDate(), //一个月多少天
    dayOfWeek: week, //当月以星期几为第一天
    dayToday: date.getDay(),//星期几
    year //年
  }
}

export const hasLogin = () =>{
	return uni.getStorageSync('token');
}

export const  dateFormat = (date: number, fortmat: string) =>{
	return dayjs(date).format(fortmat)
}

const padTo2Digits = (num: number) => {
  return num.toString().padStart(2, '0');
}
 
export const timeToMMSS = (e: number):string => {
  let time = Math.floor(e);
  const minutes = Math.floor(time / 60);
  const seconds = time % 60;
  const result = `${padTo2Digits(minutes)}:${padTo2Digits(seconds)}`;
  return result;
}

export const formatLikeCount = ({likeCount, unit = 'w'}:{likeCount: number, unit?:string}): string => {
  const threshold: number = 10000;
  if (likeCount < threshold) return likeCount.toString();
  const formattedNumber = Math.floor(likeCount / threshold);
  return `${formattedNumber}${unit}`;
}

export const getVipValidDate = () =>{
	const vipValidDate  = wx.getStorageSync('vipValidDate')
	const currentDate = Date.now();
	if (vipValidDate === null) return false
	if(vipValidDate < currentDate) return false
	return true
}

export const checkIsVip = (date: number): boolean =>{
	const currentDate = Date.now();
	if (date === null) return false
	if(date < currentDate) return false
	return true
}

export const toMilliseconds = (seconds: number): number => {
  return Math.round(seconds * 1000);
}

export const getVipDate = () =>{
	return uni.getStorageSync('vipValidDate')
}
export const formatDate = (time)=> {
    const date = new Date(time);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以加1，并用0填充
    const day = String(date.getDate()).padStart(2, '0'); // 用0填充
    // const hours = String(date.getHours()).padStart(2, '0'); // 用0填充
    // const minutes = String(date.getMinutes()).padStart(2, '0'); // 用0填充
    return `${day}/${month}/${year}`;
}